//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Fdm2dBlackScholesSolver.h"
using namespace Cephei::QL::Methods::Finitedifferences::Solvers;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSolverDesc.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSchemeDesc.h>
using namespace Cephei::QL::Processes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::CFdm2dBlackScholesSolver (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ p1, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ p2, Double correlation, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ solverDesc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CGeneralizedBlackScholesProcess^ _Cp1;
    CGeneralizedBlackScholesProcess^ _Cp2;
    CFdmSolverDesc^ _CsolverDesc;
    CFdmSchemeDesc^ _CschemeDesc;
    try
    {
#ifdef HANDLE
        _phFdm2dBlackScholesSolver = NULL;
#endif
        _Cp1 = safe_cast<CGeneralizedBlackScholesProcess^> (p1);
        _Cp1->Lock();
        Handle<QuantLib::GeneralizedBlackScholesProcess>& _p1 = static_cast<Handle<QuantLib::GeneralizedBlackScholesProcess>&> (_Cp1->GetHandle ()); 
        _Cp2 = safe_cast<CGeneralizedBlackScholesProcess^> (p2);
        _Cp2->Lock();
        Handle<QuantLib::GeneralizedBlackScholesProcess>& _p2 = static_cast<Handle<QuantLib::GeneralizedBlackScholesProcess>&> (_Cp2->GetHandle ()); 
        QuantLib::Real _correlation = (QuantLib::Real)ValueHelper::Convert (correlation); //d
        _CsolverDesc = safe_cast<CFdmSolverDesc^> (solverDesc);
        _CsolverDesc->Lock();
        QuantLib::FdmSolverDesc& _solverDesc = static_cast<QuantLib::FdmSolverDesc&> (_CsolverDesc->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc))
        {
            _CschemeDesc = safe_cast<CFdmSchemeDesc^> (schemeDesc->Value);
            _CschemeDesc->Lock();
        }
        QuantLib::FdmSchemeDesc& _schemeDesc = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc) ? static_cast<QuantLib::FdmSchemeDesc&> (_CschemeDesc->GetReference ()) : QuantLib::FdmSchemeDesc::Hundsdorfer()); //1
        _ppFdm2dBlackScholesSolver = new boost::shared_ptr<QuantLib::Fdm2dBlackScholesSolver> (new QuantLib::Fdm2dBlackScholesSolver ( _p1,  _p2,  _correlation,  _solverDesc,  _schemeDesc ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cp1 != nullptr) _Cp1->Unlock();
        if (_Cp2 != nullptr) _Cp2->Unlock();
        if (_CsolverDesc != nullptr) _CsolverDesc->Unlock();
        if (_CschemeDesc != nullptr) _CschemeDesc->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::CFdm2dBlackScholesSolver (boost::shared_ptr<QuantLib::Fdm2dBlackScholesSolver>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdm2dBlackScholesSolver = NULL;
#endif
	_ppFdm2dBlackScholesSolver = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::CFdm2dBlackScholesSolver (QuantLib::Fdm2dBlackScholesSolver& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdm2dBlackScholesSolver = NULL;
#endif
	_ppFdm2dBlackScholesSolver = new boost::shared_ptr<QuantLib::Fdm2dBlackScholesSolver> (&childNative);
    
    _Fdm2dBlackScholesSolverOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::CFdm2dBlackScholesSolver (CFdm2dBlackScholesSolver^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdm2dBlackScholesSolver = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdm2dBlackScholesSolver = new boost::shared_ptr<QuantLib::Fdm2dBlackScholesSolver> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::CFdm2dBlackScholesSolver (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdm2dBlackScholesSolver = NULL;
#endif
	if (!t->IsSubclassOf(CFdm2dBlackScholesSolver::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::CFdm2dBlackScholesSolver (QuantLib::Handle<QuantLib::Fdm2dBlackScholesSolver>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdm2dBlackScholesSolver = &childNative;
	_ppFdm2dBlackScholesSolver = &static_cast<boost::shared_ptr<QuantLib::Fdm2dBlackScholesSolver>>(childNative.currentLink());
    
    _Fdm2dBlackScholesSolverOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::CFdm2dBlackScholesSolver (QuantLib::Handle<QuantLib::Fdm2dBlackScholesSolver> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdm2dBlackScholesSolver = &childNative;
	_ppFdm2dBlackScholesSolver = &static_cast<boost::shared_ptr<QuantLib::Fdm2dBlackScholesSolver>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::CFdm2dBlackScholesSolver (QuantLib::Fdm2dBlackScholesSolver childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdm2dBlackScholesSolver = NULL;
#endif
	_ppFdm2dBlackScholesSolver = new boost::shared_ptr<QuantLib::Fdm2dBlackScholesSolver> (new QuantLib::Fdm2dBlackScholesSolver (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::~CFdm2dBlackScholesSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdm2dBlackScholesSolver != NULL)
    {
	    delete _ppFdm2dBlackScholesSolver;
        _ppFdm2dBlackScholesSolver = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::!CFdm2dBlackScholesSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdm2dBlackScholesSolver != NULL)
    {
	    delete _ppFdm2dBlackScholesSolver;
    }
}
QuantLib::Fdm2dBlackScholesSolver& Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::GetReference ()
{
    if (_ppFdm2dBlackScholesSolver == NULL) throw REFNEW NativeNullException ();
	return **_ppFdm2dBlackScholesSolver;
}
boost::shared_ptr<QuantLib::Fdm2dBlackScholesSolver>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::GetShared ()
{
    if (_ppFdm2dBlackScholesSolver == NULL) throw REFNEW NativeNullException ();
	return *_ppFdm2dBlackScholesSolver;
}
QuantLib::Fdm2dBlackScholesSolver* Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::GetPointer ()
{
    if (_ppFdm2dBlackScholesSolver == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdm2dBlackScholesSolver;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Fdm2dBlackScholesSolver>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::GetHandle ()
{
	if (_phFdm2dBlackScholesSolver == NULL)
	{
		_phFdm2dBlackScholesSolver = new Handle<QuantLib::Fdm2dBlackScholesSolver> (*_ppFdm2dBlackScholesSolver);
	}
	return *_phFdm2dBlackScholesSolver;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::HasNative () 
{
	return (_ppFdm2dBlackScholesSolver != NULL);
}

Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::DeltaXat (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdm2dBlackScholesSolver)->deltaXat ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::DeltaYat (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdm2dBlackScholesSolver)->deltaYat ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::GammaXat (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdm2dBlackScholesSolver)->gammaXat ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::GammaYat (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdm2dBlackScholesSolver)->gammaYat ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::ThetaAt (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdm2dBlackScholesSolver)->thetaAt ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver::ValueAt (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdm2dBlackScholesSolver)->valueAt ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Solvers::IFdm2dBlackScholesSolver^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2dBlackScholesSolver_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ p1, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ p2, Double correlation, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ solverDesc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc)
{
    return REFNEW CFdm2dBlackScholesSolver ( p1,  p2,  correlation,  solverDesc,  schemeDesc);
}
